﻿using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;

namespace TowerDefence
{
    public class UpdateTowerDualUpgradeUI : MonoBehaviour
    {
        #region Singleton
        // 单例模式的静态实例
        public static UpdateTowerDualUpgradeUI instance;

        // 当对象被创建时调用的方法
        public void Awake()
        {
            // 检查是否已存在实例，如果存在则输出错误信息并返回
            if (instance != null)
            {
                Debug.LogError("存在多个 UpdateTowerDualUpgradeUI 实例");
                return;
            }
            // 将当前实例赋给静态实例
            instance = this;
        }
        #endregion

        // 游戏管理器
        GameManager gameManager;

        // 升级、出售、关闭、交换按钮
        public Button upgradeButton01;
        public Button upgradeButton02;
        public Button sellButon;
        public Button closeButton;
        public Button swapButton;

        [Header("升级塔一")]
        // 塔一的图像、名称、伤害、射速、射程、描述、升级费用、出售价值的文本
        public Image towerSprite01;
        public TextMeshProUGUI nameText01;
        public TextMeshProUGUI damageText01;
        public TextMeshProUGUI fireRateText01;
        public TextMeshProUGUI rangeText01;
        public TextMeshProUGUI descriptionText01;
        public TextMeshProUGUI upgradeCostText01;
        public TextMeshProUGUI sellValueText01;

        [Space]
        [Header("升级塔二")]
        // 塔二的图像、名称、伤害、射速、射程、描述、升级费用的文本
        public Image towerSprite02;
        public TextMeshProUGUI nameText02;
        public TextMeshProUGUI damageText02;
        public TextMeshProUGUI fireRateText02;
        public TextMeshProUGUI rangeText02;
        public TextMeshProUGUI descriptionText02;
        public TextMeshProUGUI upgradeCostText02;

        [Space]
        [Header("当前塔")]
        // 当前塔的状态窗口的图像、名称、伤害、射速、射程、描述的文本
        public GameObject currentStatWindow;
        public Image towerSprite;
        public TextMeshProUGUI nameText;
        public TextMeshProUGUI damageText;
        public TextMeshProUGUI fireRateText;
        public TextMeshProUGUI rangeText;
        public TextMeshProUGUI descriptionText;

        [Space]
        // 交换按钮的图像
        public Image swap;

        // 当对象启用时调用的方法
        private void OnEnable()
        {
            // 关闭当前塔的状态窗口
            currentStatWindow.SetActive(false);
        }

        // 当对象被创建时调用的方法
        private void Start()
        {
            // 查找并获取游戏管理器的实例
            gameManager = FindObjectOfType<GameManager>();

            // 关闭当前对象
            gameObject.SetActive(false);
        }


        public void UpdateUI(TowerData upgradeData01, TowerData upgradeData02, TowerData currentData)
        {
            // 更新第一个升级选项的界面信息
            if (upgradeData01 != null)
            {
                towerSprite01.sprite = upgradeData01.icon;
                nameText01.text = upgradeData01.towerName;
                damageText01.text = "伤害: " + upgradeData01.minDamage + " - " + upgradeData01.maxDamage;
                fireRateText01.text = "射速: " + upgradeData01.fireRate;
                rangeText01.text = "射程: " + upgradeData01.range;
                descriptionText01.text = upgradeData01.description;
                upgradeCostText01.text = upgradeData01.cost.ToString();
                sellValueText01.text = upgradeData01.sellValue.ToString();
            }

            // 更新第二个升级选项的界面信息
            if (upgradeData02 != null)
            {
                towerSprite02.sprite = upgradeData02.icon;
                nameText02.text = upgradeData02.towerName;
                damageText02.text = "伤害: " + upgradeData02.minDamage + " - " + upgradeData02.maxDamage;
                fireRateText02.text = "射速: " + upgradeData02.fireRate;
                rangeText02.text = "射程: " + upgradeData02.range;
                descriptionText02.text = upgradeData02.description;
                upgradeCostText02.text = upgradeData02.cost.ToString();
            }

            // 更新当前塔的界面信息
            towerSprite.sprite = currentData.icon;
            nameText.text = currentData.towerName;
            damageText.text = "伤害: " + currentData.minDamage + " - " + currentData.maxDamage;
            fireRateText.text = "射速: " + currentData.fireRate;
            rangeText.text = "射程: " + currentData.range;
            descriptionText.text = currentData.description;
        }

        // 切换按钮状态的方法
        public void SwapButton()
        {
            // 切换当前统计窗口的显示状态
            currentStatWindow.SetActive(!currentStatWindow.activeInHierarchy);

            // 根据当前统计窗口的状态设置按钮颜色
            if (currentStatWindow.activeInHierarchy)
                swap.color = Color.gray; // 窗口激活时按钮颜色为灰色
            else
                swap.color = Color.white; // 窗口未激活时按钮颜色为白色
        }

        // 升级按钮01的方法
        public void UpgradeButton01()
        {
            // 调用GameManager中的UpgradeTowerButton01方法
            gameManager.UpgradeTowerButton01();
        }

        // 升级按钮02的方法
        public void UpgradeButton02()
        {
            // 调用GameManager中的UpgradeTowerButton02方法
            gameManager.UpgradeTowerButton02();
        }

        // 出售按钮的方法
        public void SellButton()
        {
            // 调用GameManager中的SellTowerButton方法
            gameManager.SellTowerButton();
        }

    }
}
